import { VuexModule, getModule, Module, Mutation, Action } from 'vuex-module-decorators';
import { TagView } from '/@/script/types/Tags';
import store from '/@/store';


import { hotModuleUnregisterModule } from '/@/utils/helper/vuexHelper';

const NAME = 'app';
hotModuleUnregisterModule(NAME);
@Module({ dynamic: true, namespaced: true, store, name: NAME })
class App extends VuexModule {
  // Page loading status
  private pageLoadingState = false;

  get getPageLoading() {
    return this.pageLoadingState;
  }
  @Mutation
  commitPageLoadingState(loading: boolean): void {
    this.pageLoadingState = loading;
  }
  @Action
  public async setPageLoadingAction(loading: boolean): Promise<void> {
    this.commitPageLoadingState(loading);
  }

  // resize
  private resize: number = 1
  get getResize() {
    return this.resize
  }
  @Mutation
  commitResize() {
    if (this.resize > 1000000) this.resize = 1
    this.resize++
  }
  // collapsed
  private collapsed: boolean = false
  get getCollapsed() {
    return this.collapsed
  }
  @Mutation
  commitCollapsed() {
    this.collapsed = !this.collapsed
  }


  private testViews: any[] = []
  // get getTestViews() {
  //   return this.testViews
  // }
  @Mutation
  commitAddTestView(view: any) {
    // if (this.testViews.some(v => v.path === view.path)) return
    // this.testViews.push(
    //     Object.assign({}, view, {
    //       title: view.title || view.meta?.title || 'no-name'
    //     })
    // )

    this.testViews.push(view)
    // console.log("啊哈哈哈", view)
  }
  @Action
  public async ACTION_ADD_TEST_VIEW(view: any): Promise<void> {
    this.commitAddTestView(view);
  }
  // tabs 部分开始=========================================================
  private visitedViews: TagView[] = []
  get getVisitedViews() {
    return this.visitedViews
  }
  @Mutation
  commitAddVisitedView(view: TagView) {
    if (this.visitedViews.some(v => v.path === view.path)) return
    this.visitedViews.push(
      Object.assign({
        path: view.path,
        fullPath: view.fullPath,
        name: view.name,
        query: view.query,
        params: view.params,
        meta: view.meta,
      } as TagView, {
        title: view.title || view.meta?.title || 'no-name'
      })
    )
  }
  @Mutation
  commitDelVisitedView(view: TagView) {
    for (const [i, v] of this.visitedViews.entries()) {
      if (v.path === view.path) {
        this.visitedViews.splice(i, 1)
        break
      }
    }
  }
  @Mutation
  commitDelOtherVisitedViews(view: TagView) {
    this.visitedViews = this.visitedViews.filter(v => {
      return v.meta?.affix || v.path === view.path
    })
  }
  @Mutation
  commitDelAllVisitedViews() {
    this.visitedViews = this.visitedViews.filter(item => item.meta?.affix == true)
  }
  @Mutation
  commitUpdateVisitedView(view: TagView) {
    for (let v of this.visitedViews) {
      if (v.path === view.path) {
        v = Object.assign(v, {
          path: view.path,
          fullPath: view.fullPath,
          name: view.name,
          query: view.query,
          params: view.params,
          meta: view.meta,
        } as TagView)
        break
      }
    }
  }

  private cachedViews: (string | undefined)[] = []
  get getCachedViews() {
    return this.cachedViews
  }
  @Mutation
  commitAddCacheView(view: TagView) {
    if (view.name === null) return
    if (this.cachedViews.includes(view.name?.toString())) return
    if (!view.meta?.noCache) {
      this.cachedViews.push(view.name?.toString())
    }
  }
  @Mutation
  commitDelCachedView(view: TagView) {
    if (view.name === null) return
    const index = this.cachedViews.indexOf(view.name?.toString())
    index > -1 && this.cachedViews.splice(index, 1)
  }
  @Mutation
  commitDelOtherCachedViews(view: TagView) {
    if (view.name === null) return
    const index = this.cachedViews.indexOf(view.name?.toString())
    if (index > -1) {
      this.cachedViews = this.cachedViews.slice(index, index + 1)
    } else {
      // if index = -1, there is no cached tags
      this.cachedViews = []
    }
  }
  @Mutation
  commitDelAllCachedViews() {
    this.cachedViews = []
  }


  @Action
  public async ACTION_ADD_VIEW(view: TagView): Promise<void> {
    this.commitAddVisitedView(view);
    this.commitAddCacheView(view);
  }
  @Action
  public async ACTION_ADD_VISITED_VIEW(view: TagView): Promise<void> {
    this.commitAddVisitedView(view);
  }
  @Action
  public async ACTION_DEL_VIEW(view: TagView): Promise<void> {
    this.commitDelVisitedView(view);
    this.commitDelCachedView(view);
  }
  @Action
  public async ACTION_DEL_OTHER_VIEW(view: TagView): Promise<void> {
    this.commitDelOtherVisitedViews(view);
    this.commitDelOtherCachedViews(view);
  }
  @Action
  public async ACTION_DEL_CACHED_VIEW(view: TagView): Promise<void> {
    this.commitDelCachedView(view);
  }
  @Action
  public async ACTION_DEL_ALL_VIEWS(): Promise<void> {
    this.commitDelAllVisitedViews();
    this.commitDelAllCachedViews();
  }
  @Action
  public async ACTION_DEL_ALL_CACHED_VIEWS(): Promise<void> {
    this.commitDelAllCachedViews();
  }
  @Action
  public async ACTION_UPDATE_VISITED_VIEW(view: TagView): Promise<void> {
    this.commitUpdateVisitedView(view);
  }
  // tabs 部分结束=========================================================
}
export const appStore = getModule<App>(App);
